package com.gzy.wbapplication.modules.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import com.gzy.wbapplication.R;
import com.gzy.wbapplication.StartActivity;
import com.gzy.wbapplication.events.LoginCompleteEvent;
import com.gzy.wbapplication.events.LogoutEvent;
import com.gzy.wbapplication.manager.AuthManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        findViewById(R.id.rl_back).setOnClickListener(view -> {
            finish();
        });

        findViewById(R.id.btn_logout).setOnClickListener(view -> {
            logout();
        });

        EventBus.getDefault().register(this);
    }

    private void logout() {
        AuthManager.logout(this);

        startActivity(new Intent(this, StartActivity.class));

        EventBus.getDefault().post(new LogoutEvent());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(LogoutEvent message) {
        finish();
    }
}
